/*
 * BTServerNetworkManagerTester.java
 *      Author: nfk
 *        Date: Sep 21, 2008
 *        
 *     MIDlets:
 *          BTServerNetworkManagerTester
 */

package at.momberban.game.me.tests;


import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import at.momberban.game.me.BTServerNetworkManager;
import at.momberban.game.me.ServerNetworkManager;


/**
 * A simple server network manager test MIDlet.
 * @author nfk
 */
public final class BTServerNetworkManagerTester extends MIDlet implements CommandListener {

    private List                 list;
    private Command              exit;
    private Command              start;
    private Command              stop;

    private Display              display;
    private ServerNetworkManager nm;


    public BTServerNetworkManagerTester() {
        this.list = new List("BTServerNetworkManagerTester", List.IMPLICIT);
        this.exit = new Command("Exit", Command.EXIT, 1);
        this.start = new Command("Start", Command.OK, 1);
        this.stop = new Command("Stop", Command.CANCEL, 1);
        this.list.addCommand(this.exit);
        this.list.addCommand(this.start);
        this.list.setCommandListener(this);

        this.display = Display.getDisplay(this);
        this.nm = BTServerNetworkManager.getManager();
    }


    /**
     * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
     */
    protected void destroyApp(final boolean arg0) throws MIDletStateChangeException {
        if (BTServerNetworkManager.supportsBluetooth()) {
            this.nm.disconnect();
        }
    }


    /**
     * @see javax.microedition.midlet.MIDlet#pauseApp()
     */
    protected void pauseApp() {
    // Ignore
    }


    /**
     * @see javax.microedition.midlet.MIDlet#startApp()
     */
    protected void startApp() throws MIDletStateChangeException {
        if (!BTServerNetworkManager.supportsBluetooth()) {
            final Alert noBluetooth = new Alert("Error", "No bluetooth available", null, AlertType.ERROR);
            noBluetooth.addCommand(exit);
            noBluetooth.setCommandListener(this);
            noBluetooth.setTimeout(Alert.FOREVER);
            display.setCurrent(noBluetooth);
        } else {
            display.setCurrent(list);
        }
    }


    /**
     * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command,
     *      javax.microedition.lcdui.Displayable)
     */
    public void commandAction(final Command arg0, final Displayable arg1) {
        if (arg0 == this.exit) {
            try {
                destroyApp(false);
                notifyDestroyed();
            } catch (final MIDletStateChangeException ex) {
                // Ignore
            }
        } else if (arg0 == this.start) {
            this.list.removeCommand(this.start);
            this.list.addCommand(this.stop);

            new Thread(new Runnable() {

                /**
                 * @see java.lang.Runnable#run()
                 */
                public void run() {
                    try {
                        nm.listen();
                    } catch (IOException ex) {
                        // Ignore
                    }
                }

            }).start();

        } else if (arg0 == this.stop) {
            this.list.removeCommand(this.stop);
            this.list.addCommand(this.start);

            this.nm.disconnect();
        }
    }

}

/* End of BTServerNetworkManagerTester.java */
